package com.atguigu.newmvc2.controller;


import com.atguigu.newmvc2.pojo.Employee;
import com.atguigu.newmvc2.service.EmpService;
import com.atguigu.newmvc2.utils.R;
import com.atguigu.newmvc2.vo.req.AddEmpVo;

import com.atguigu.newmvc2.vo.req.UpdateEmpVo;
import com.atguigu.newmvc2.vo.resp.RespEmpVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Tag(name = "员工信息表")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping("/getEmp/{id}")
    @Operation(summary = "查看单个员工信息")
    public R getEmp(@PathVariable Integer id){
        Employee employee = empService.getEmpById(id);
        RespEmpVo respEmpVo = new RespEmpVo();
        BeanUtils.copyProperties(employee,respEmpVo);
        return R.ok(respEmpVo);
    }
    @PostMapping("/addEmp")
    @Operation(summary = "添加员工信息")
    public R addEmp(@RequestBody @Valid AddEmpVo addEmpVo){
        Employee employee = new Employee();
        BeanUtils.copyProperties(addEmpVo,employee);
        empService.addEmp(employee);
        return R.ok();
    }

    @DeleteMapping("/delEmp/{id}")
    @Operation(summary = "删除员工信息")

    public R delEmp(@PathVariable Integer id){
        empService.delEmpById(id);
        return R.ok();
    }

    @PutMapping("/updateEmp")
    @Operation(summary = "修改员工信息")

    public R updateEmp(@RequestBody @Valid UpdateEmpVo updateEmpVo){
        Employee employee = new Employee();
        BeanUtils.copyProperties(updateEmpVo,employee);
        empService.updateEmp(employee);
        return R.ok();
    }
    @GetMapping("/getAllEmp")
    @Operation(summary = "查看所有员工信息")

    public R<List<RespEmpVo>> getAllEmp(){
        List<Employee> employeeList = empService.getAllEmp();
        ArrayList<RespEmpVo> respEmpVoList = new ArrayList<>();
        employeeList.forEach(employee -> {
            RespEmpVo respEmpVo = new RespEmpVo();
            BeanUtils.copyProperties(employee,respEmpVo);
            respEmpVoList.add(respEmpVo);
        });
        return R.ok(respEmpVoList);
    }
}
